package com.aigou.admin.controller;

import com.aigou.admin.annotation.ResponseResult;
import com.aigou.admin.entity.*;
import com.aigou.admin.entity.vo.ResponseVo;
import com.aigou.admin.service.ShopCartService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author: xsc_hunter
 * @Date: 2024/12/4) 下午3:56
 * @Version: 1.0
 */
@RestController
@RequestMapping("/shopCart")
@AllArgsConstructor
@ResponseResult
public class ShopCartController {

    private final ShopCartService shopCartService;

    @RequestMapping("/page")
    public ResponseVo page(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String current = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");
        PageEntity<ShopCart> shopCartsByPage = shopCartService.findShopCartByPage(Integer.parseInt(current), Integer.parseInt(pageSize));
        //转换为JSON,通过流发送给客户端
        return ResponseVo.buildSuccess(shopCartsByPage);
    }

    @RequestMapping("/save")
    public ResponseVo save(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        String cart_id = req.getParameter("cart_id");
        String user_id = req.getParameter("user_id");
        ShopCart shopCart = new ShopCart(id,
                cart_id,
                user_id);
        Boolean save = shopCartService.save(shopCart);
        return ResponseVo.buildSuccess(save);
    }

    @RequestMapping("/findById")
    public ResponseVo findById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        // 获得id
        String id = req.getParameter("id");
        ShopCart shopCart = shopCartService.findById(id);
        //转换为JSON,通过流发送给客户端
        return ResponseVo.buildSuccess(shopCart);
    }

    @RequestMapping("/findAll")
    public ResponseVo findAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<ShopCart> shopCarts = shopCartService.findAll();
        //转换为JSON,通过流发送给客户端
        return ResponseVo.buildSuccess(shopCarts);
    }

    @RequestMapping("/delete")
    public ResponseVo delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        Boolean remove = shopCartService.remove(id);
        return ResponseVo.buildSuccess(remove);
    }

    @RequestMapping("/update")
    public ResponseVo update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        String cart_id = req.getParameter("cart_id");
        String user_id = req.getParameter("user_id");
        ShopCart shopCart = new ShopCart(id,
                cart_id,
                user_id);
        Boolean update = shopCartService.update(shopCart);
        return ResponseVo.buildSuccess(update);
    }
}
